$OpenBSD: patch-cdda2wav_sndconfig_c,v 1.3 2014/11/03 09:12:41 ratchov Exp $
--- cdda2wav/sndconfig.c.orig	Sun Nov 29 23:07:37 2009
+++ cdda2wav/sndconfig.c	Fri Oct 31 10:18:09 2014
@@ -51,6 +51,10 @@ static	UConst char sccsid[] =
 # endif
 #endif
 
+#ifdef	HAVE_SNDIO
+#include <sndio.h>
+#endif
+
 #include "mytype.h"
 #include "byteorder.h"
 #include "lowlevel.h"
@@ -85,7 +89,11 @@ static PLAYLISTSTRUCTURE PlayList[FRAGMENTS + 1];
 static unsigned BufferInd;
 #   endif /* defined __EMX__ */
 
+#if	defined(HAVE_SNDIO)
+static char snd_device[200] = SIO_DEVANY;
+#else
 static char snd_device[200] = SOUND_DEV;
+#endif
 
 int
 set_snd_device(devicename)
@@ -161,6 +169,11 @@ check_winsound_caps(bits, rate, channels)
 snd_pcm_t	*pcm_handle;
 #endif
 
+#ifdef	HAVE_SNDIO
+struct sio_hdl *hdl;
+struct sio_par par;
+#endif
+
 #if	defined	HAVE_OSS && defined SNDCTL_DSP_GETOSPACE
 audio_buf_info abinfo;
 #endif
@@ -172,6 +185,41 @@ init_soundcard(rate, bits)
 {
 #ifdef	ECHO_TO_SOUNDCARD
 	if (global.echo) {
+# if	defined(HAVE_SNDIO)
+		hdl = sio_open(snd_device, SIO_PLAY, 0);
+		if (hdl == NULL) {
+			errmsg("Cannot open sound device '%s'.\n", snd_device);
+			return (1);
+		}
+		sio_initpar(&par);
+		par.bits = bits;
+		par.sig = 1;
+		par.le = SIO_LE_NATIVE;
+		par.pchan = 2;
+		par.rate = rate;
+		par.appbufsz = 44100 / 4;
+		if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
+			errmsg("Cannot set sound parameters for '%s'.\n", snd_device);
+			sio_close(hdl);
+			hdl = NULL;
+			return (1);
+		}
+		if (par.bits != bits || par.sig != 1 || par.le != SIO_LE_NATIVE ||
+		    par.pchan != 2 || par.rate != (int)rate) {
+			errmsg("Unsupported sound parameters for '%s'.\n", snd_device);
+			sio_close(hdl);
+			hdl = NULL;
+			return (1);
+		}
+		if (!sio_start(hdl)) {
+			errmsg("Couldn't start sound device '%s'.\n", snd_device);
+			sio_close(hdl);
+			hdl = NULL;
+			return (1);
+		}
+
+# else /* HAVE_SNDIO */
+
 # if	defined(HAVE_OSS) && HAVE_OSS == 1
 		if (open_snd_device() != 0) {
 			errmsg("Cannot open sound device '%s'.\n", snd_device);
@@ -543,6 +591,7 @@ init_soundcard(rate, bits)
 #   endif /* CYGWIN Windows sound */
 #  endif /* else SUN audio */
 # endif /* else HAVE_OSS */
+# endif /* else HAVE_SNDIO */
 	}
 #endif /* ifdef ECHO_TO_SOUNDCARD */
 	return (0);
@@ -586,6 +635,12 @@ close_snd_device()
 	return (0);
 #else
 
+# if	defined(HAVE_SNDIO)
+	if (hdl != NULL) {
+		sio_close(hdl);
+		hdl = NULL;
+	}
+#else
 # if	defined(__CYGWIN32__) || defined(__MINGW32__)
 	waveOutReset(0);
 	return (waveOutClose(DeviceID));
@@ -610,6 +665,7 @@ close_snd_device()
 #   endif /* !QNX */
 #  endif /* !EMX */
 # endif /* !Cygwin32 */
+# endif
 #endif /* ifdef ECHO_TO_SOUNDCARD */
 }
 
@@ -620,6 +676,10 @@ write_snd_device(buffer, todo)
 {
 	int	result = 0;
 #ifdef	ECHO_TO_SOUNDCARD
+#if	defined(HAVE_SNDIO)
+	if (hdl == NULL || !sio_write(hdl, buffer, todo))
+		return (1);
+#else
 #if	defined(__CYGWIN32__) || defined(__MINGW32__)
 	MMRESULT	mmres;
 
@@ -706,6 +766,7 @@ outside_loop:
 	;
 #endif	/* !defined __EMX__ */
 #endif	/* !defined __CYGWIN32__ */
+#endif
 #endif	/* ECHO_TO_SOUNDCARD */
 	return (result);
 }
